package com.wenx.remotestrategy;

import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * 模拟10000个请求同时访问
 */
public class TestMain {

    private static final Integer num = 10000;

    private static CountDownLatch countDownLatch = new CountDownLatch(num);


    public static void main(String[] args) throws InterruptedException {
        Instant start = Instant.now();

        Map map = new ConcurrentHashMap();

        for (int i = 0; i < num; i++) {
            new Thread(() -> {
                countDownLatch.countDown();
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                AccessService accessService = new AccessService();
                Object remoteResult = accessService.getRemoteResult("111");
                map.put(Thread.currentThread().getName(),remoteResult);
            },"name"+i).start();
        }
        Instant end = Instant.now();
        Duration between = Duration.between(start, end);
        System.out.println("between = " + between.toMillis());
        System.out.println("results = " + map.size());
        Thread.sleep(Long.MAX_VALUE);
    }

}
